export default {
    data() {
        return {
            student_id: 0,
            ruleForm: {
                name:'',
                card:'',
                mobile:'',
                parent_name:'',
                parent_mobile:'',
                teacher_name:'',
                check_time:''
            },
            rules: {
                name: [
                    {required: true, message: '请输入参赛人姓名', trigger: 'blur'},
                ],
                card: [
                    {required: true, message: '请输入参赛人身份证号', trigger: 'blur'},
                ],
                teacher_name: [
                    {required: true, message: '请输入指导老师姓名', trigger: 'blur'},
                ],
                mobile: [
                    {required: true, message: '请输入参赛人手机号', trigger: 'blur'},
                ],
                check_time: [
                    {required: true, message: '请选择学琴时长', trigger: 'blur'},
                ],
            },
            identity:1,                     //1,超级管理员,2,审核员,3专家,4机构
            region_name_list:[],            //赛区列表
            seller_name_list:[],            //赛区列表
            region_id:'',                   //选择赛区id
            seller_id:'',                   //选择机构id
            time:['1','2','3','4','5','6','7','8','9','10'],    //学琴时长

        }
    },

    //进入页面加载
    mounted: function () {
        var that = this;
        //在缓存中获取值
        that.user_name = sessionStorage.getItem("user_name");
        that.user_id = sessionStorage.getItem("user_id");
        that.token = sessionStorage.getItem("access-token");
        that.identity = sessionStorage.getItem("identity");
        if ((!that.token) || (!that.user_id) || (!that.user_name)) {
            that.$router.push('/login');
        }

        //加载轮播详情
        if (that.$route.query.student_id) {
            that.student_id = that.$route.query.student_id;
            that.detail();
        }

        that.regionList();
    },

    //方法
    methods: {
        //请求api
        submitForm(formName) {
            var that = this;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (that.student_id === 0) {
                        that.add();
                    } else {
                        that.edit();
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },

        /**
         * 添加
         * @returns {boolean}
         */
        add: function () {
            var that = this;
            if(that.identity === '1'){
                if(that.region_id === ''){
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '请选择赛区' }`
                    });
                    return false;
                }
                if(that.seller_id === ''){
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '请选择机构' }`
                    });
                    return false;
                }
            }
            //请求的数据
            var formData = {};
            formData.token = that.token;
            formData.user_id = that.user_id;
            formData.region_id = that.region_id;
            formData.seller_id = that.seller_id;
            formData.real_name = that.ruleForm.name;
            formData.identity_number = that.ruleForm.card;
            formData.study_len = that.ruleForm.check_time;
            formData.mobile = that.ruleForm.mobile;
            formData.teacher = that.ruleForm.teacher_name;
            formData.parent_name = that.ruleForm.parent_name;
            formData.parent_mobile = that.ruleForm.parent_mobile;

            //请求api
            that.$http.post(that.adminApi.api_url + "/User_Identity/add", formData, {
                emulateJSON: true
            }).then(
                function (res) {
                    console.log(res);
                    // 处理成功的结果
                    // that.$message({
                    //     type: 'success',
                    //     message: `操作提示: ${ '添加成功' }`
                    // });
                    that.$router.push({path: '/seller/student_video',query: {student_id: res.body}});
                },
                function (res) {
                    console.log(res);
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ res.body.msg }`
                    });
                });
        },

        /**
         * 详情
         */
        detail: function () {
            var that = this;
            //请求的数据
            var formData = {};
            formData.token = that.token;
            formData.id = that.student_id;

            //请求邀请者列表
            that.$http.post(that.adminApi.api_url + "/User_Identity/show_edit", formData, {
                emulateJSON: true
            }).then(
                function (res) {
                    that.region_id = res.body.region_id;
                    if(res.body.region_id){
                        that.sellerList();
                    }
                    that.seller_id = res.body.seller_id;
                    that.ruleForm.name = res.body.real_name;
                    that.ruleForm.card = res.body.identity_number;
                    that.ruleForm.mobile = res.body.mobile;
                    that.ruleForm.teacher_name = res.body.teacher;
                    that.ruleForm.parent_name = res.body.parent_name;
                    that.ruleForm.parent_mobile = res.body.parent_mobile;
                    that.ruleForm.check_time = res.body.study_len;

                },
                function () {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '处理异常' }`
                    });
                });
        },

        /**
         * 修改
         * @returns {boolean}
         */
        edit: function () {
            var that = this;
            if(that.identity === '1'){
                if(that.region_id === ''){
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '请选择赛区' }`
                    });
                    return false;
                }
                if(that.seller_id === ''){
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '请选择机构' }`
                    });
                    return false;
                }
            }
            //请求的数据
            var formData = {};
            formData.token = that.token;
            formData.user_id = that.user_id;
            formData.region_id = that.region_id;
            formData.seller_id = that.seller_id;
            formData.real_name = that.ruleForm.name;
            formData.identity_number = that.ruleForm.card;
            formData.study_len = that.ruleForm.check_time;
            formData.mobile = that.ruleForm.mobile;
            formData.teacher = that.ruleForm.teacher_name;
            formData.parent_name = that.ruleForm.parent_name;
            formData.parent_mobile = that.ruleForm.parent_mobile;
            formData.id = that.student_id;

            //请求API
            that.$http.post(that.adminApi.api_url + "/User_Identity/edit", formData, {
                emulateJSON: true
            }).then(
                function (res) {
                    // 处理成功的结果
                    // that.$message({
                    //     type: 'success',
                    //     message: `操作提示: ${ '修改成功' }`
                    // });
                    that.$router.push({path: '/seller/student_video',query: {student_id: that.student_id}});
                },
                function () {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '处理异常' }`
                    });
                });
        },

        //返回
        cancel: function () {
            var that = this;
            that.$router.push({path: '/seller/student'});
        },


        /**
         * 赛区列表
         */
        regionList :function () {
            var that = this;
            //请求的数据
            var formData = {};
            formData.token = that.token;

            //请求api
            that.$http.post(that.adminApi.api_url + "/Region/index", formData, {
                emulateJSON: true
            }).then(
                function(res) {
                    // 处理成功的结果
                    if(res.body.list){
                        for (var i in res.body.list){
                            that.region_name_list.push ({
                                id:res.body.list[i].id,
                                name:res.body.list[i].name,
                            })
                        }
                    }

                },
                function() {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '处理异常' }`
                    });
                });
        },

        /**
         * 选择完毕
         */
        checkOver:function () {
            let that =this;
            that.sellerList();
        },

        /**
         * 机构列表
         */
        sellerList :function () {
            var that = this;
            //请求的数据
            var formData = {};
            formData.token = that.token;
            formData.region_id = that.region_id;
            //请求api
            that.$http.post(that.adminApi.api_url + "/Seller/index", formData, {
                emulateJSON: true
            }).then(
                function(res) {
                    // 处理成功的结果
                    if(res.body.list){
                        for (var i in res.body.list){
                            that.seller_name_list.push ({
                                id:res.body.list[i].id,
                                name:res.body.list[i].name,
                            })
                        }
                    }

                },
                function() {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '处理异常' }`
                    });
                });
        }
    }
}